$(document).ready(function () {
	//variable para hacer las validaciones del email
	var emailreg = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;
	  
	  var vars = [], hash;
      var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('#');
	    for(var i = 0; i < hashes.length; i++) {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
     var user=vars["user"];
      var localizacion;
	$('#map').gmap('getCurrentPosition', function(position, status) {
			if ( status === 'OK' ) {
			localizacion = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
			
			}
		});
	  
	   $("#boton").click(function(e) {  
       	 
       var friend = $(".friend").val();
		    
	        $(".error").remove();
	       if( friend == "Add Friend..." || !emailreg.test(friend) ){
	            $(".friend").focus().after("<span class='error'>Ingress a correct email</span>");
	            return false;
	         }    
	         
	        Parse.$= jQuery; 
			//Parse.initialize("cIsgepqAZm90F2mWdiJ9nP5HkrdG3nBcs98yJrVA", "CYI3htcqoz9bERjSCTpqu5FOWjhkkl72CgEenoLD");	
		   Parse.initialize("UCyMTCIXJqmFk3oLy4CwkxXv6oymztzqqoQNWAux", "BBYPhajAwBgLFzDTXvnx4MaReUn8Kke2KkwQ4yTT");
		   //Parse.initialize("gLxhWhhBK7SPLZ7Jevs6q2lcLBlhje3l3sQlyS0e", "nPvJsuTXpFeKvNyDjZg8ks8sYwt2nntVOi9hr91J");	
						   
			var UserRegister = Parse.Object.extend("Contact");
			var query = new Parse.Query(UserRegister);
			query.equalTo("email", friend);
			query.find({
		  		success: function(results) {	
				      var contact = results[0];
				      if(contact){
					        Parse.$= jQuery; 
						    //Parse.initialize("cIsgepqAZm90F2mWdiJ9nP5HkrdG3nBcs98yJrVA", "CYI3htcqoz9bERjSCTpqu5FOWjhkkl72CgEenoLD");	
						   Parse.initialize("UCyMTCIXJqmFk3oLy4CwkxXv6oymztzqqoQNWAux", "BBYPhajAwBgLFzDTXvnx4MaReUn8Kke2KkwQ4yTT");
						   //Parse.initialize("gLxhWhhBK7SPLZ7Jevs6q2lcLBlhje3l3sQlyS0e", "nPvJsuTXpFeKvNyDjZg8ks8sYwt2nntVOi9hr91J");
						   var Friends = Parse.Object.extend("Friends");
						   var friend1 = new Friends();
							 
							friend1.set("friend1", friend);
							friend1.set("friend2", user);
							friend1.set("lat",0);
							friend1.set("lng",0);
						    friend1.save();
						    var friend2 = new Friends();
							 
							friend2.set("friend1", user);
							friend2.set("friend2", friend);
							friend2.set("lat",0);
							friend2.set("lng",0);
						    friend2.save();
						document.location.href='home.html?user='+user+'#settings';	
					   }
					   else{
					      	$(".friend").focus().after("<span class='error'>Ingress a correct email</span>");
	            			return false;
					   }
				 },
				 error: function(results, error) {
				 	document.location.href='home.html?user='+user+'#settings';	
		            alert("Error: " + error.code + " " + error.message);
		          }
	});
				
	});
	
	$("#actualizaLoc").click(function(e) {
		Parse.$= jQuery; 
		 //Parse.initialize("cIsgepqAZm90F2mWdiJ9nP5HkrdG3nBcs98yJrVA", "CYI3htcqoz9bERjSCTpqu5FOWjhkkl72CgEenoLD");	
		Parse.initialize("UCyMTCIXJqmFk3oLy4CwkxXv6oymztzqqoQNWAux", "BBYPhajAwBgLFzDTXvnx4MaReUn8Kke2KkwQ4yTT");
		//Parse.initialize("gLxhWhhBK7SPLZ7Jevs6q2lcLBlhje3l3sQlyS0e", "nPvJsuTXpFeKvNyDjZg8ks8sYwt2nntVOi9hr91J");
		var Prueba2 = Parse.Object.extend("Friends");
		var query = new Parse.Query(Prueba2);
    // ckessler is the current user
    	query.equalTo("friend2", user);
   	 	query.find({
      		success: function(results) {
        		$.each(results, function(i,result){
            		result.save(null, {
              			success: function(result) {
                			result.set("lat", localizacion.lat());
                			result.set("lng", localizacion.lng());
                			result.save();
              			}
            		});

        		});  

      		}
    	});
	});
	
      
});
